/*
* Copyright (c) 2011 adVolition
*
* Permission is hereby granted, free of charge, to any person
* obtaining a clone of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* clone, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* Except as contained in this notice, the name(s) of the above
* copyright holders shall not be used in advertising or otherwise to
* promote the sale, use or other dealings in this Software without prior
* written authorization.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
 */

package NeuralNetwork;
import com.thoughtworks.xstream.annotations.*;
/**
 * A Edge class is a collection of nodes and edges.
 */
@XStreamAlias("edge") public class Edge {

    @XStreamAlias("input") @XStreamAsAttribute private int _input;
    @XStreamAlias("output") @XStreamAsAttribute private int _output;
    @XStreamAlias("threshold") @XStreamAsAttribute private Double _threshold;
    @XStreamAlias("flux") @XStreamAsAttribute private Double _flux;

    protected transient Bundle _parentBundle;
    protected transient Mutability _parentFidelity;

    public Edge(Bundle parentBundle) {
        _parentBundle = parentBundle;
        _parentFidelity = parentBundle.getFidelity();
        configureObject();
    }

    public Edge(int input, int output, Double threshold, Double flux, Bundle parentBundle) {
        _input = input;
        _output = output;
        _threshold = threshold;
        _flux = flux;
        _parentBundle = parentBundle;
        _parentFidelity = parentBundle.getFidelity();
        configureObject();
    }

    public Object readResolve() {
        configureObject();
        return this;
    }
    
    /** Used by class constructor and readResolve routines to ensure that newly created
     * objects are configured properly.
     * @return 
     **/    
    private void configureObject() {   
        if (_parentBundle == null) _parentBundle = new Bundle();
        if (_parentFidelity == null) _parentFidelity = new Mutability();
        if (_input == 0) _input = 0;
        if (_output == 0) _output = 0;
        if (_threshold == null) _threshold = 0.0;
        if (_flux == null) _flux = 0.1;  
    }
    
    
    public void reset() {
        
    }

    public void computeFutureState(Double timeIncrement) {

    }

    public void implementFutureState() {

    }

    /**
     * This is an error-prone method for cloning the object.  It is used to generate mutant robot offspring.
     * @return
     */
    public Edge getClone(Bundle parentBundle) {
        return new Edge(cloneInput(), cloneOutput(), cloneThreshold(), cloneCurrent(), parentBundle);
    }

    private int cloneInput() {
        if (Math.random() < _parentFidelity.getMutateEdge()) {
            if (Math.random() < _parentFidelity.getLinkToParent()) {
                return _parentBundle.getRandomNodeID();
            } else {
                return -1 * _parentBundle.getParent().getRandomNodeID();
            }
        }
        return _input; 
    }

    private int cloneOutput() {
        if (Math.random() < _parentFidelity.getMutateEdge()) {
            if (Math.random() < _parentFidelity.getLinkToParent()) {
                return _parentBundle.getRandomNodeID();
            } else {
                return -1 *  _parentBundle.getParent().getRandomNodeID();
            }
        }
        return _output;
    }
    
    private Double cloneThreshold() {
        if (Math.random() > _parentFidelity.getMutateThreshold()) {
            return Math.random() * 2.0 - 1.0;
        }
        return _threshold;
    }

    private Double cloneCurrent() {
        if (Math.random() > _parentFidelity.getMutateFlux()) {
            return Math.random() * 2.0 - 1.0;
        } 
        return _flux;
    }

    /**
     * Assigns a parent to each of the child and its children.
     * @param parent
     */
    public void assignParent(Bundle parent) {
        _parentBundle = parent;
        _parentFidelity = parent.getFidelity();
    }

}
